package com.hsw.habit.controller;

import com.hsw.habit.frame.annotation.CurrentUser;
import com.hsw.habit.frame.annotation.UserId;
import com.hsw.habit.model.JsonData;
import com.hsw.habit.model.TaskItem;
import com.hsw.habit.model.User;
import com.hsw.habit.model.bean.ListResponse;
import com.hsw.habit.service.TaskService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    TaskService service;

    @RequestMapping("/index")
    public JsonData taskList(@CurrentUser User user, Integer type,Integer startRow) {
        return JsonData.success(service.findTaskByType(user.getId(),startRow,type));
    }

    @RequestMapping("/add")
    public JsonData addItem(@CurrentUser User user,TaskItem taskItem) {
        if (null != taskItem) {
            taskItem.setUserId(user.getId());
        }
        boolean res = service.addItemByParams(taskItem);
        if (res) {
            return JsonData.success();
        } else {
            return JsonData.error();
        }
    }

    @RequestMapping("/itemDetail")
    public JsonData getItem(Long id) {
        return JsonData.success(service.findTaskItemById(id));
    }

    @RequestMapping("/update")
    public JsonData updateItem(TaskItem taskItem) {
        boolean res = service.updateById(taskItem);
        if (res) {
            return JsonData.success();
        } else {
            return JsonData.error();
        }
    }

    @RequestMapping("remove")
    public JsonData removeItem(Long id) {
        boolean res = service.deleteById(id);
        if (res) {
            return JsonData.success();
        } else {
            return JsonData.error();
        }
    }

}
